#ifndef UARTDEVICE_H
#define UARTDEVICE_H

/**
 * @copyright Copyright (c) 2025 
 * CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author zhouyan
 * @brief 串口设备类
*/
#include "phylink.h"
#include "tools/m_mutex.h"
#include "tools/m_sleep.h"

class UartDevice : public IPhyLink
{
public:
    UartDevice(const char *dev_name, uint32_t baudrate, char parity = 'N', bool open_daly = false);
    ~UartDevice();

    virtual bool isActived(void) const;
    virtual bool connect(int timeout);
    virtual void disconnect(void);

    virtual bool tryLock(void);
    virtual void lock(void);
    virtual void unlock(void);

    uint32_t read(void *buf, uint32_t max_byte) override;
    virtual uint32_t read(void *buf, uint32_t max_byte, uint32_t timeout);
    virtual int async_read(uint8_t *RecvBuf, uint32_t max_byte, uint32_t i_RecvTimeoutS);
    virtual uint32_t write(const void *buf, uint32_t max_byte);
    virtual void clear(void);
    void restore(void) override;
    virtual bool SwitchSettings(ComSettings *settings, bool isReopen = true) ;

private:
    void openUart();
private:
    MMutex              mtx_;
    int                 fd_;
    bool                open_daly_;        
    std::string         dev_name_;
    uint32_t            baudrate_;
    char                parity_;
};

#endif // UARTDEVICE_H
